package com.school.campus.service.impl;

import java.util.Collections;
import java.util.List;

import com.school.campus.domain.vo.ConversationsVO;
import com.school.common.utils.DateUtils;
import com.school.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.school.campus.mapper.ConversationsMapper;
import com.school.campus.domain.Conversations;
import com.school.campus.service.IConversationsService;

/**
 * 单聊会话（存储一对一聊天关系）Service业务层处理
 *
 * @author wwj
 * @date 2025-11-07
 */
@Service
public class ConversationsServiceImpl implements IConversationsService {
    @Autowired
    private ConversationsMapper conversationsMapper;

    /**
     * 查询单聊会话（存储一对一聊天关系）
     *
     * @param id 单聊会话（存储一对一聊天关系）主键
     * @return 单聊会话（存储一对一聊天关系）
     */
    @Override
    public Conversations selectConversationsById(Long id) {
        return conversationsMapper.selectConversationsById(id);
    }

    /**
     * 查询单聊会话（存储一对一聊天关系）列表
     *
     * @param conversations 单聊会话（存储一对一聊天关系）
     * @return 单聊会话（存储一对一聊天关系）
     */
    @Override
    public List<Conversations> selectConversationsList(Conversations conversations) {
        return conversationsMapper.selectConversationsList(conversations);
    }

    /**
     * 新增单聊会话（存储一对一聊天关系）
     *
     * @param conversations 单聊会话（存储一对一聊天关系）
     * @return 结果
     */
    @Override
    public int insertConversations(Conversations conversations) {
        conversations.setCreateTime(DateUtils.getNowDate());
        return conversationsMapper.insertConversations(conversations);
    }

    /**
     * 修改单聊会话（存储一对一聊天关系）
     *
     * @param conversations 单聊会话（存储一对一聊天关系）
     * @return 结果
     */
    @Override
    public int updateConversations(Conversations conversations) {
        return conversationsMapper.updateConversations(conversations);
    }

    /**
     * 批量删除单聊会话（存储一对一聊天关系）
     *
     * @param ids 需要删除的单聊会话（存储一对一聊天关系）主键
     * @return 结果
     */
    @Override
    public int deleteConversationsByIds(Long[] ids) {
        return conversationsMapper.deleteConversationsByIds(ids);
    }

    /**
     * 删除单聊会话（存储一对一聊天关系）信息
     *
     * @param id 单聊会话（存储一对一聊天关系）主键
     * @return 结果
     */
    @Override
    public int deleteConversationsById(Long id) {
        return conversationsMapper.deleteConversationsById(id);
    }

    @Override
    public List<ConversationsVO> selectConversationsVOList(Conversations conversations) {
        conversations.setUser1Id(SecurityUtils.getUserId());
        return conversationsMapper.selectConversationsVOList(conversations);
    }

    @Override
    public int privateChat(Long userId) {
        Long currentUserId = SecurityUtils.getUserId();
        Conversations conversations = conversationsMapper.selectConversationsByUserIds(currentUserId, userId);
        if (conversations != null) {

            return -1;
        }

        Conversations newConversations = new Conversations();
        newConversations.setUser1Id(currentUserId);
        newConversations.setUser2Id(userId);

        newConversations.setCreateTime(DateUtils.getNowDate());
        return conversationsMapper.insertConversations(newConversations);
    }
}
